/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon
 												
Class							:				CaseBaseBehavior
 
Function						: 				
*********************************************************************************/

package cbr.episodicCBR;

import org.jdom.Element;

import planninglanguage.goals.Goal;
import planninglanguage.plans.Plan;
import util.XMLWriter;

public class CaseBaseBehavior {

	String m_ID;
	Goal m_goal;
	Plan m_plan;
	boolean modified;
	
	public boolean getModified()
	{
		return modified;
	}
	public void setModified(boolean mod)
	{
		modified = mod;
	}
	public void reset()
	{
		modified = false;
	}
	
	public CaseBaseBehavior(String ID,Goal g,Plan p) {
		m_ID = ID;
		m_goal = g;
		m_plan = p;
	}
	
	public Plan getPlan() {
		return m_plan;
	}
	
	public Goal getGoal() {
		return m_goal;
	}
	
	/*
	 * This method saves a behavior to a file in XML format
	 */
	public void saveToXML(XMLWriter w)
	{
		w.tag("behavior id=\"" + m_ID + "\"");
		{
			m_goal.writeXML(w);
			
			w.tag("plan");
			{
				m_plan.writeXML(w);
			}
			w.tag("/plan");
		}
		w.tag("/behavior");
	}
	
	public static CaseBaseBehavior loadFromXML(Element e) throws Exception {
		assert e.getName().equals("behavior") : 
			"CaseBaseBehavior.loadFromXML: Invalid XML Element" + e.getName();
		
		String id = e.getAttributeValue("id");
		Goal goal = Goal.loadGoalFromXML(e.getChild("goal"));
		Plan plan = Plan.loadPlanFromXML((Element) e.getChild("plan").getChildren().get(0));
		
		return new CaseBaseBehavior(id, goal, plan);
	}
}
